from pyecharts.charts import Pie
from pyecharts import options as opts
from house_data.data_get import data_connect

bh = 'SELECT book_name, book_hot FROM boy_hot_range;'
gh = 'SELECT book_name, book_hot FROM girl_hot_range;'
bn = 'SELECT book_name, book_hot FROM boy_new_range;'
gn = 'SELECT book_name, book_hot FROM girl_new_range;'
data1 = data_connect.seach_sql(bh)
x=[]
y=[]
for a in data1:
    x.append(a[0])
    y.append(a[1])

data2 = data_connect.seach_sql(gh)
for a in data2:
    x.append(a[0])
    y.append(a[1])

data3 = data_connect.seach_sql(bn)
for a in data3:
    x.append(a[0])
    y.append(a[1])

data4 = data_connect.seach_sql(gn)
for a in data4:
    x.append(a[0])
    y.append(a[1])

x_data = x
y_data = y
data_pair = [list(z) for z in zip(x_data, y_data)]
data_pair.sort(key=lambda x: x[1])

(
    Pie(init_opts=opts.InitOpts(bg_color="#2c343c"))
    .add(
        series_name="热度(单位：万)",
        data_pair=data_pair,
        rosetype="radius",
        radius="55%",
        center=["50%", "50%"],
        label_opts=opts.LabelOpts(is_show=False, position="center"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="热门&新秀小说热度一览",
            pos_left="20",
            pos_top="20",
            title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    )
    .set_series_opts(
        tooltip_opts=opts.TooltipOpts(
            trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
        ),
        label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
    )
    .render("../templates/book_index.html")
)